<%@page contentType="text/html; charset=utf-8" language="java" import="cn.ac.cintcm.synonymous.*"  errorPage="" %>
<%@page import="java.util.List,java.sql.*,net.sf.json.*" %>
<%
Connection conn = OracleDb.getConnection();
NameFieldDao  categoryDao = new NameFieldDao(conn, NameFieldDao.CATEGORY);
NameFieldDao  sourceDao = new NameFieldDao(conn, NameFieldDao.SOURCE);
NameRecordDao recordDao = new NameRecordDao(conn, categoryDao, sourceDao);
List overViews = null;
try {
    overViews = recordDao.getOutline();
    conn.close();
} catch (SQLException e) {
    e.printStackTrace();
}
%>
<html>
<head> 
	<title>工具</title>
	<link rel="stylesheet" type="text/css" href="syn.css" />
	<script type="text/javascript" src="json.js"></script>
	<script type="text/javascript" src="prototype.js"></script>
	<script type="text/javascript">
		var selectedTableId;
		var selectedTableName;
		var ii=0;
		var synType='f';
		var synTypeAll='t';
		var param = new Param();
		var basePath ="comparisonName.jsp";
		function Item(tableId) {
			this.tableId=tableId;
			this.cmpType = 'f';
			this.categories = new Array();
			this.add=function(category){
				if (!this.categories[category]) {
				    this.categories[category] = true; 
			  } 
			   return this.categories[category];				
			}
			
		}  

		function Param() {
			this.items = new Array();
			this.equal = true;
			this.add=function(table) {
				if (!this.items[table]) {
					  this.items[table] = true;
				} 
				return this.items[table];
			}
		}
		
		function createXMLHttpRequest() {
	    	if (window.ActiveXObject) {
	    		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    	}
	    	else if (window.XMLHttpRequest) {
	        	xmlHttp = new XMLHttpRequest();                
	    	}
		}
		function addDict(id, name){
			 var clObj = $("categories");
		   var clNum = clObj.childNodes.length;
	       for(var i=0;i<clNum;i++)
		   { 
		    clObj.removeChild(clObj.childNodes[0]);
		   }
			selectedTableId = id;
			selectedTableName = name;
			var url = "getCategory.jsp?tableId=" + selectedTableId;
			createXMLHttpRequest();
	    	xmlHttp.open("GET", url, true);
	    	xmlHttp.onreadystatechange = callbackForAdd;
	    	xmlHttp.send(null);
		}
	
		function callbackForAdd() {
	    	if (xmlHttp.readyState == 4) {
	    		if (xmlHttp.status == 200) {
	        		parseResultsForAdd();
	        	}
	     	}
		}
	
		function parseResultsForAdd() {
			var results = eval(xmlHttp.responseText);
			clearResults();
			/**********添加标题********/
			var inObj = $("categories");
			var row = inObj.insertRow();
			var cell1 = row.insertCell();
			var cell2 = row.insertCell();
			var cell3 = row.insertCell();
			cell1.innerHTML="类选择";
			cell2.innerHTML="类名称";
			cell3.innerHTML="词条数";
			/********************/
			for (var i=0; i< results.length; i++) {
				  addRow(results[i]);
			}
			
		}
		function addRow(rs) {
			var inObj = $("categories");
			var row = inObj.insertRow();
			row.className="odd";
			var cell1 = row.insertCell();
			var cell2 = row.insertCell();
			var cell3 = row.insertCell();
			
			var checkObj = document.createElement("input");
			checkObj.name = "caName";
			checkObj.type = "checkbox";
			checkObj.value = rs.id;
			checkObj.cname = rs.name;
			/**************
			var itemHistory = $(rs.id+"-"+selectedTableId);
			if(itemHistory != null)
			{
			   checkObj.checked= true;
			 }
			 *************/
			checkObj.onclick = function(evn) {getSelectedRow(this);};
			var textNode1 = document.createTextNode(rs.name);
			var textNode2 = document.createTextNode(rs.count);
			cell1.appendChild(checkObj);
			cell2.appendChild(textNode1);
			cell3.appendChild(textNode2);
	 }
	 function clearResults() {
			var tableBody = document.getElementById("categories");
			while( tableBody.childNodes.length > 1 ) {
				tableBody.removeChild(tableBody.childNodes[1]);
			}
	 }
	 
	 /*******被选类加入页面************/
	  function getSelectedRow (s) {
			var itemHistory = $(s.value+"-"+selectedTableId);
			var key = selectedTableId+"_s";
			if(itemHistory == null){
			 /**************当选了分项时 默认把表选上********/
			    if($(key))
			     {
			      var sObj = $(key);
			      if(!sObj.checked)sObj.checked=true;
			     }
		     /********************/
				var item = new Item(selectedTableId);
				item.add(s.value);
				
				var inObj = $("selectedItems");
				/*******标题部分!******/
				if(ii==0){
				 var rowTitle = inObj.insertRow();
				 
				 var cellTitle = rowTitle.insertCell();
				  cellTitle.colSpan="2";
				  cellTitle.align="center"   
				 cellTitle.innerHTML="<strong>已经选项</strong>";
				}
				/***********/
				
				var row = inObj.insertRow();
				var cell1 = row.insertCell();
				var cell2 = row.insertCell();
				row.className="odd";
				var textNode1 = document.createTextNode(selectedTableName);
				var textNode2 = document.createTextNode(s.cname);	   	
				cell1.appendChild(textNode1);
				cell2.appendChild(textNode2);
				row.id= s.value+"-"+selectedTableId;
				row.name='selNum';
				row.selectedTableId=selectedTableId;
	   			ii++;
		 }else{
			  if(!s.checked)
				{
					$("selectedItems").deleteRow(itemHistory.rowIndex);
					 var key = selectedTableId+"_s";
			   /*********如果表的所选分项都去掉 那么也去掉表的选择************/
			    if($(key))
			     {
			      var sObj = $(key);
			      sObj.checked=isTab(selectedTableId);
			     }
					
				}
		}
	   	
	  }
	  function compare() {
	  		basePath ="comparisonName.jsp";
	  		var pars=postData();
	  		setInit("enquData");
	  		sub(basePath,pars,showResponse);
	  }
	  function exp()
	  {
	  		window.location="exp.jsp?"+postData();
	  }
	  
	  function postData()
	  {
	  	/****获得所选项****/
	  	var selItems = $("selectedItems");
	  	var categoriesId = '';
		   var selNum = selItems.rows.length;
	       for(var i=1;i<selNum;i++)
		   {
		    var d = selItems.rows(i).id;
		    categoriesId +='{"sourceId":"'+selItems.rows(i).selectedTableId+'","category":"'+d.split("-")[0]+'"},';
		   }
		   if(categoriesId !='')
		   categoriesId = "["+categoriesId.substring(0,categoriesId.length-1)+"]";
	   	//window.location.href = "index.jsp?query="+map.toJSONString();
	   	
	   	 var stName = document.getElementsByName("stName");
	   	 var stSize = stName.length;
	     var sourceId ='';
	   
	     for(var i=0;i<stSize;i++)
	     {
	     	if(stName[i].checked)
	     	 {
	     	 	var taname = stName[i].value;
	     	    var cmpType = $(taname+'_c').value;
	     	    sourceId +='{"sourceId":"'+taname+'","cmpType":"'+cmpType+'"},';
	     	}
	     }
	       if(sourceId !='')
		   			sourceId = "["+sourceId.substring(0,sourceId.length-1)+"]";
		   	 var state = document.getElementsByName("state");
		   	 var stateTem =0;
			  for(var i=0;i<state.length;i++)
		     {
		     	if(state[i].checked)
		     	 {
		     	 	stateTem=state[i].value;
		     	}
		     }
		     
		   	var pars = "query="+categoriesId+"&sourceData="+sourceId+"&state="+stateTem;
		   	return pars;
	}
	  
	 function sub(url,pars,onC){
	    var myAjax = new Ajax.Request(
		url,
		{
		method: 'post',
		parameters: pars,
		onComplete: onC
		});
	}
function showResponse(originalRequest)
{
//put returned XML in the textarea
var results= originalRequest.responseText;

 /************删除以前数据*************/
			 var clObj = $("enquData");
			  var clNum = clObj.childNodes.length;
			     for(var i=0;i<clNum;i++)
			  { 
			   clObj.removeChild(clObj.childNodes[0]);
			  }
   /*****************************/
          
if(results != '' && results.indexOf('data')>0){
	var obj = eval("["+results+"]");
	if(obj[0].data)
	{
	if(!obj[0].data[0])
		setNull('enquData');;
	var size = obj[0].data[0].length;
	var dataSize = obj[0].data.length;
	var dataObj = $('enquData');
	var row = dataObj.insertRow();
	/********显示记录数***********/
	 var cell1 = row.insertCell();
	 cell1.colspan=size+"";
	 cell1.innerHTML="<font color=#3300FF >共["+obj[0].tableSize+"]条记录</font>";

	/********表头!!**************/
	 row = dataObj.insertRow();
    for(var i=0 ;i<size;i++){
	    var cell1 = row.insertCell();
	    cell1.innerHTML="<strong>"+obj[0].head[i]+"<strong>";	
	    cell1.innerHTML = cell1.innerHTML+"&nbsp;";
    }
	/*******************/
	
	for(var y=0 ;y<dataSize;y++){
		 row = dataObj.insertRow();
		 for(var i=0 ;i<size;i++){
		    var cell1 = row.insertCell();
		    cell1.innerHTML =obj[0].data[y][i];	
		   // alert("y:"+y+"  i:"+i+"   Data:"+obj[0].data[y][i]);
		    cell1.innerHTML = cell1.innerHTML+"&nbsp;"
	     }
    }
	} 
}

}

/*******搜索被选项里面是否还有改表记录 有返回true 反之********/
function isTab(tabId){
	var selItems = $("selectedItems");
	var categoriesId = '';
	var selNum = selItems.rows.length;
	for(var i=1;i<selNum;i++)
	{
	 var key = selItems.rows(i).selectedTableId;
	 if(tabId == key)
	 	return true;
	}   
  return false;
}

function setInit(cName){
var dataObj = $(cName);
 var row = dataObj.insertRow();
 var cell1 = row.insertCell();
 cell1.innerHTML="<img src='page-loading.gif' />";
}
function setNull(cName){
var dataObj = $(cName);
 var row = dataObj.insertRow("tr");
 var cell1 = row.insertCell();
 cell1.innerHTML="<font color=red >无记录</font>";
}  
	function changeName()
	{
		var ii = 0;
		var viewName = '';
	    var viewNameF = ''; 
	   var stName = document.getElementsByName("stName");
	   	 var stSize = stName.length;
	   	
	     for(var i=0;i<stSize;i++)
	     {
	     	if(stName[i].checked)
	     	{
	     	 	ii++;
	     	 	var taname = stName[i].value;
	     	    var cmpType = $(taname+'_c').value;
	     	     viewName += $(taname+'_c').options[$(taname+'_c').selectedIndex].text+"和";
				if(cmpType == synType)
			   	    {
			   	     	viewNameF +="异名和";
			   	    }
			   	    else if(cmpType == synTypeAll){
			   	    	viewNameF +="全部和";
			   	    }
			   	    else
			   	    {
			   	    	viewNameF +="正名和";
			   	    }
	     	} 	
	    }
	    
	  
	    if(ii>=2)
	    {
	      if(viewNameF != '')
	       viewNameF = viewNameF.substring(0,viewNameF.length-1);
	    if(viewName != '')
	         viewName = viewName.substring(0,viewName.length-1);
	        
		     for(var i=0;i<3;i++)
		     {	
		        if(i != 0)
			     	{
			     	  $("a"+i).innerHTML="";
					  $("a"+i).innerHTML=viewNameF;
				   } 	 		
				else
				{
				 $("a"+i).innerHTML="";
				 $("a"+i).innerHTML=viewName;
				 }
		     }
	     }
	}
	</script>
</head>
<body>
<h2>中医药标准工具</h2>
<table id="xxa" width="200" border="0" cellpadding="0" cellspacing="0" valign="top">
  <tr>
    <td valign="top">
     <table>
     	<tr>
     	<td>
		      <table border="1" class="sofT" cellspacing="0">
					   <thead>
						    <tr class="helpHed">
						      <th>标准名称</th>
						      <th>词条数</th>
						      <th>表选择</th>
						      <th>比较内容</th>
						      <th>类选择</th>
						    </tr>
					   </thead>
					   <%
					   if (overViews != null) {
					   	   for (int i=0; i< overViews.size(); i++) {
					   	       OverView ov = (OverView) overViews.get(i);
					   	       
					   %>
							   <tr>
							     <td><%=ov.getName()%></td>
							     <td><%=ov.getCount()%></td>
							     <td><input type="checkbox" onpropertychange="changeName();" value="<%=ov.getId()%>" id="<%=ov.getId()%>_s" name="StName"></td>
							     <td >
							     	   <select id="<%=ov.getId()%>_c" name="cmpType" onchange="changeName();">
							     	    	<%
							     	    	List choices = Choice.getChoices();
							     	    	String state ="";
							     	    	for(int j= 0; j < choices.size(); j++) {
							     	    	if(((Choice)choices.get(j)).getId()==NameRecord.FORMAL)
							     	    		state ="selected";
							     	    	    out.write("<option "+state+" value='"+((Choice)choices.get(j)).getId()+"'>"+((Choice)choices.get(j)).getName()+"</option>");
							     	    	state="";
							     	    	}
							     	    	%>
							     	   </select>
							     </td>
							     <td><a href="#" onClick="addDict(<%=ov.getId()%>, '<%=ov.getName()%>');">类选择</a></td>
							   </tr>
					   <%  }
					   }
					   %>
					</table>
					</td>
				</tr> 
				<tr><td >
				<table  id="selectedItems" border="1" class="sofT" cellspacing="0" width="200px"> 
						</table></td>
				</tr>
				<tr><td class="helpHed"><span id="a0">正名和正名</span>全部相同<input type="radio" name="state" checked value="0" />    <span id="a1">异名和异名</span>相同<input type="radio" name="state" value="1" />   <span id="a2">异名和异名</span>不同<input type="radio"  name="state" value="2" /><input type="button" value="比较" onClick="compare();">  <input type="button" value="清空" onClick="window.location.reload();"><input type="button" value="csv导出" onClick="exp();"></td></tr>
				<tr><td>
					<table id="enquData" border="1" class="sofT" cellspacing="0" >
				    </table>
		    		</td>
		    	</tr>
			</table>
			</td>
    	<td valign="top">
    	<table  id="categories"  border="1" class="sofT" cellspacing="0" width="200px"> 
		</table>
	   	   </td>
  	  </tr>
</table>
</body>
</html>
